$(function () {
    var queryUrl = '../modulerecord/list';
    var Id = getQueryString('Id');
    if(Id){
        queryUrl += '?Id=' + Id;
    }
    $("#jqGrid").Grid({
        url: queryUrl,
        colModel: [
            {
                label: '台账类型', name: 'type', index: 'TYPE', width: 80,
                formatter: function (value) {
                    return sysEnums.MODULE_RECORD_TYPE[value];
                }
            },
            {label: '项目名称', name: 'proName', index: 'PRO_NAME', width: 80},

            {label: '开始时间', name: 'startDate', index: 'START_DATE', width: 80,
                formatter: function (value) {
                    return transDate(value, 'yyyy-MM-dd');
                }
            },
            {label: '结束时间', name: 'endDate', index: 'END_DATE', width: 80,
                formatter: function (value) {
                    return transDate(value, 'yyyy-MM-dd');
                }
            },
            {label: '实施单位', name: 'expUnit', index: 'EXP_UNIT', width: 80},
            {label: '实施背景', name: 'implBack', index: 'IMPL_BACK', width: 120},
            {label: '实施方式', name: 'impMode', index: 'IMP_MODE', width: 80},
            {label: '实施方案', name: 'proPlan', index: 'PRO_PLAN', width: 200},
            {label: '验证方案', name: 'valPlan', index: 'VAL_PLAN', width: 200},
            {label: '项目收益', name: 'proReve', index: 'PRO_REVE', width: 80},
            {label: '改善效果-改良改善', name: 'impEff', index: 'IMP_EFF', width: 120},
            {label: '改善措施-改良改善', name: 'proMethod', index: 'PRO_METHOD', width: 120},
            {label: '备注', name: 'remark', index: 'REMARK', width: 80},
            {label: '维修人员名称', name: 'repairUserName', index: 'REPAIR_USER_NAME', width: 80},
            {
                label: '审核详情', name: '', index: '', width: 80,
                formatter: function (value, col, row) {
                    return "<button class='btn btn-outline btn-info' id='recordsText' onclick='vm.shviewOption(\"" + escape(JSON.stringify(row)) + "\")'>&nbsp;详情</button>";
                }
            },
            {
                label: '预览/下载', name: 'fileUrl', width: 40, align: 'center',
                formatter: function (value, col, row) {
                    if (row.file8d == null) {
                        return "<button title='预览' class='btn btn-outline btn-info' onclick='vm.eyeFile(\"" + value + "\")'><i class='fa fa-eye'></i></button>";
                    } else {
                        value = row.file8d;
                        var png = value.split(".").indexOf(("pdf")) < 0
                        var jpg = value.split(".").indexOf(("png")) < 0
                        var pdf = value.split(".").indexOf(("jpg")) < 0
                        if (png && jpg && pdf) {
                            return vm.eyeFile();
                        } else {
                            return "<button title='预览' class='btn btn-outline btn-info' onclick='fileCommonHandle(\"" + value + "\")'><i class='fa fa-eye'></i></button>";
                        }
                    }

                }
            },
            {label: 'id', name: 'id', index: 'ID', key: true, hidden: true},
        ]
    });
});

let vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        show: false,
        moduleRecord: {
            createDate: '',
            type: '',
            expUnit: '',
            impMode: '',
            proReve: '',
            impEff: '',
            valPlan: '',
            proPlan: '',
            proMethod: '',
            implBack: '',
            proName: '',
            endDate: '',
            startDate: '',
            fileName: '',
            fileUrl: '',
            deptCode: '',
            approvalStatus: '',
            approvalTime: '',
            approvalUser: '',
            updateUser: '',
            updateDate: '',
            createUser: '',
            delFlag: '',
            remark: '',
            repairUserName: '',
            repairUserId: '',
            repairUserCode: '',
            id: '',
        },
        ruleValidate: {
            name: [
                {required: true, message: '名称不能为空', trigger: 'blur'}
            ]
        },
        //模具台账类型 0 改良改善 1自主维修
        moduleTypeList: enumsToList("MODULE_RECORD_TYPE"),
        q: {
            name: ''
        }
    },
    methods: {
        onselect: function (e) {
            if(e*1){
                vm.show = false
            }else{
                vm.show = true
            }
        },
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.moduleRecord = {
                createDate: '',
                type: '',
                expUnit: '',
                impMode: '',
                proReve: '',
                impEff: '',
                valPlan: '',
                proPlan: '',
                proMethod: '',
                implBack: '',
                proName: '',
                endDate: '',
                startDate: '',
                fileName: '',
                fileUrl: '',
                deptCode: '',
                approvalStatus: '',
                approvalTime: '',
                approvalUser: '',
                updateUser: '',
                updateDate: '',
                createUser: '',
                delFlag: '',
                remark: '',
                repairUserName: '',
                repairUserId: '',
                repairUserCode: '',
                id: '',
            };
        },
        update: function (event) {
            let id = getSelectedRow("#jqGrid");
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id);
        },
        saveOrUpdate: function (event) {
            let url = vm.moduleRecord.id == (null || '') ? "../modulerecord/save" : "../modulerecord/update";
            Ajax.request({
                url: url,
                params: JSON.stringify(vm.moduleRecord),
                type: "POST",
                contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
            });
        },
        del: function (event) {
            let ids = getSelectedRows("#jqGrid");
            if (ids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                Ajax.request({
                    url: "../modulerecord/delete",
                    params: JSON.stringify(ids),
                    type: "POST",
                    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });
            });
        },
        getInfo: function (id) {
            Ajax.request({
                url: "../modulerecord/info/" + id,
                async: true,
                successCallback: function (r) {
                    vm.moduleRecord = r.moduleRecord;
                }
            });
        },
        reload: function (event) {
            vm.showList = true;
            let page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {'name': vm.q.name},
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
        },
        reloadSearch: function () {
            vm.q = {
                name: ''
            };
            vm.reload();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        },
        handleError: function (file) {
            this.$Notice.warning({
                title: '文件格式不正确',
                desc: '文件 ' + file.name + ' 格式不正确，请上传格式正确的表格。'
            });
        },
        handleFormatError: function (file) {
            this.$Notice.warning({
                title: '文件格式不正确',
                desc: '文件 ' + file.name + ' 格式不正确，请上传格式正确的表格。'
            });
        },
        handleMaxSize: function (file) {
            this.$Notice.warning({
                title: '超出文件大小限制',
                desc: '文件 ' + file.name + ' 太大，不能超过 2M。'
            });
        },
        handleBeforeUpload: function() {
            this.$refs.uploadFile.clearFiles();
            this.$Loading.start();
        },
        handleSuccess2: function (res, file) {
            this.$Loading.finish();
            iview.Message.success('文件上传成功');
            vm.reload();
        },
        handleSuccess: function (res, file) {
            this.$Loading.finish();
            iview.Message.success('文件上传成功');
            vm.moduleRecord.fileUrl = file.response.url;
            var relName = file.response.name;
            vm.moduleRecord.fileName = relName;
        },
        eyeFile: function (url) {
            if (url) {
                window.open(url);
            } else {
                alert('还未上传文件');
            }
        },
        //审核详情
        shviewOption: function (res) {
            let data = JSON.parse(unescape(res))
            if (!data.approvalName) {
                data.approvalName = "-"
            }
            let dom = "<div class='details' >" +
                "<table>" +
                "<tr><td>审核人：</td><td>" + data.approvalName + "</td></tr>" +
                "<tr><td>审核状态：</td><td>" + sysEnums.CHECK_FLAG[data.approvalStatus] + "</td></tr>" +
                "<tr><td>审核时间：</td><td>" + transDate(data.approvalTime, "yyyy-MM-dd") + "</td></tr>" +
                "<tr><td>创建时间：</td><td>" + transDate(data.createDate, "yyyy-MM-dd") + "</td></tr>" +
                "</table></div>"
            layer.open({
                type: 1,
                title: "详情",
                closeBtn: 0,
                area: ['450px'],
                offset: '10vh',
                shadeClose: true,
                skin: 'yourclass',
                content: dom
            });
        },
        ylFile: function (e) {
            var dom = '<iframe frameborder="0" align="left" width="100%" height="100%"\n' +
                'scrolling="auto" marginWidth=0 marginHeight=0 ' +
                ' style="width: 100%;height: 100%" ' +
                'src="' + e + '"></iframe>'

            if (e) {
                layer.open({
                    type: 1,
                    title: "详情",
                    closeBtn: 0,
                    area: ['60vw', "80vh"],
                    offset: '10vh',
                    shadeClose: true,
                    skin: 'yourclass',
                    content: dom
                });
            } else {
                alert('还未上传文件');
            }


        },
        check: function () {
            var data = getSelectedRowData("#jqGrid");
            if (data.id == null) {
                return;
            }

            if (data.approvalStatus !== '未审核') {
                alert("此条记录已审核！", function () {
                    vm.reload();
                });
                return;
            }
            vm.moduleRecord = {approvalStatus: 1, id: data.id};
            confirm('确定要审核选中的记录？', function () {
                Ajax.request({
                    url: "../modulerecord/check",
                    params: JSON.stringify(vm.moduleRecord),
                    type: "POST",
                    contentType: "application/json",
                    successCallback: function (r) {
                        alert('审核成功', function (index) {
                            vm.reload();
                        });
                    }
                });
            });
        },
    }
});
